package net.hn.hnms.biz.plan.company.service.impl;

import cn.hutool.core.date.DateUtil;
import net.hn.hnms.biz.plan.company.service.CompanyStatisticService;
import net.hn.hnms.biz.plan.mapper.CompanyStatisticMapper;
import net.hn.hnms.biz.plan.vo.MonthValue;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.biz.plan.vo.FootAgeVo;
import net.hn.hnms.biz.plan.mapper.StatisticMapper;
import net.hn.hnms.biz.plan.vo.PlanCompleteStatisticVO;
import net.hn.hnms.biz.plan.vo.PlanStatisticVO;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CompanyStatisticServiceImpl implements CompanyStatisticService {
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private CompanyStatisticMapper companystatisticMapper;

    @Override
    public FootAgeVo drivageCount(Integer month, String mineCode) {
        if (month == null || (month >12&&month<0)){
            LocalDate date = LocalDate.now();
            month = date.getMonthValue();
        }
        FootAgeVo completeInfo = new FootAgeVo();
        if (month != null){

        Date[] dateRange = DateUtils.getDateRange(null, month);
             completeInfo = statisticMapper.drivageCount(dateRange[0], dateRange[1],mineCode);

        }
        return completeInfo;
    }

    @Override
    public FootAgeVo miningCount(Integer month, String mineCode) {
        if (month == null || (month >12&&month<0)){
            LocalDate date = LocalDate.now();
            month = date.getMonthValue();
        }
        FootAgeVo completeInfo = new FootAgeVo();
        if (month != null){

            Date[] dateRange = DateUtils.getDateRange(null, month);
            completeInfo = statisticMapper.miningCount(dateRange[0], dateRange[1],mineCode);

        }
        return completeInfo;
    }


    @Override
    public List<PlanStatisticVO> outputCountmonthAndYear(Integer month, String mineCode) {
        ArrayList<PlanStatisticVO> list = new ArrayList<>();
        LocalDate date = LocalDate.now();
// 获得当前的月和日
        if (month == null || (month > 12 && month < 0)) {
            month = date.getMonthValue();
        }
        Date[] dateRange = DateUtils.getDateRange(null, month);
        PlanCompleteStatisticVO completeInfo = statisticMapper.planComplete3(dateRange[0], dateRange[1], mineCode);
        PlanStatisticVO planStatisticVO = new PlanStatisticVO();
        PlanStatisticVO planStatisticVO2 = new PlanStatisticVO();

        int year = DateUtil.year(new Date());
        Date beginDate = DateUtil.beginOfDay(DateUtil.parseDate(year + "-01-01"));
        Date endDate = DateUtil.endOfDay(DateUtil.parseDate(year + "-12-31"));
        PlanCompleteStatisticVO completeInfo2 = statisticMapper.planComplete3(beginDate, endDate, mineCode);

        planStatisticVO.setDataName("月产");
        planStatisticVO.setPlanTotal(completeInfo.getPlanOutput().divide(Constants.TEN_THOUSAND, 2, RoundingMode.CEILING));
        planStatisticVO.setCompleteTotal(completeInfo.getOutput().divide(Constants.TEN_THOUSAND, 2, RoundingMode.CEILING));
        BigDecimal decimal = new BigDecimal(0);
        if (completeInfo.getPlanOutput().compareTo(new BigDecimal(0)) != 0) {
            decimal = (completeInfo.getOutput().divide(completeInfo.getPlanOutput(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_HALF_UP);
        }
        planStatisticVO.setPercent(decimal);
        list.add(planStatisticVO);
        planStatisticVO2.setDataName("年产");
        planStatisticVO2.setPlanTotal(completeInfo2.getPlanOutput().divide(Constants.TEN_THOUSAND, 2, RoundingMode.CEILING));
        planStatisticVO2.setCompleteTotal(completeInfo2.getOutput().divide(Constants.TEN_THOUSAND, 2, RoundingMode.CEILING));
        BigDecimal decimal2 = new BigDecimal(0);
        if (completeInfo2.getPlanOutput().compareTo(new BigDecimal(0)) != 0) {
            decimal2 = (completeInfo2.getOutput().divide(completeInfo2.getPlanOutput(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_HALF_UP);
        }
        planStatisticVO2.setPercent(decimal2);
        list.add(planStatisticVO2);

        return list;
    }



    @Override
    public List<MonthValue> washing(Integer month, String companyCode) {
//        Date[] dateRange = DateUtils.getDateRange(DateUtil.year(new Date()), month);
//        statisticMapper.washingOfMonth(companyCode);
//        return null;

        Date[] dateRange = DateUtils.getDateRange(DateUtil.year(new Date()), month);
        Map<Integer, BigDecimal> planSaleOfMonth = companystatisticMapper.planWashingOfMonth(dateRange[0], dateRange[1],companyCode).stream().collect(Collectors.toMap(MonthValue::getMonth, MonthValue::getTotal));

//        DynamicDataSourceContextHolder.push("shipped");
        Map<Integer, BigDecimal> realSaleOfMonth = companystatisticMapper.realWashingOfMonth(dateRange[0], dateRange[1],companyCode).stream().collect(Collectors.toMap(MonthValue::getMonth, MonthValue::getTotal2));
//        DynamicDataSourceContextHolder.clear();

        List<MonthValue> result = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            BigDecimal plan = planSaleOfMonth.getOrDefault(i, BigDecimal.ZERO);
            BigDecimal sale = realSaleOfMonth.getOrDefault(i, BigDecimal.ZERO);
            MonthValue vo = new MonthValue(i);
            vo.setTotal(plan);
            vo.setTotal2(sale);
            vo.setTotal3(plan.compareTo(new BigDecimal("0"))==0?new BigDecimal("0"):sale.divide(plan,2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")));
            if (month != null && month != i) {
                continue;
            }
            result.add(vo);
        }
        return result;
    }

    @Override
    public List<String> selectMineCode() {
        List<String> strings = statisticMapper.selectMineCode();
        return strings;
    }@Override
    public List<String> selectcompanyCode() {
        List<String> strings = statisticMapper.selectcompanyCode();
        return strings;
    }

    @Override
    public MonthValue washingOfyesterDay(String companyCode) {
        MonthValue monthValue = companystatisticMapper.washingOfyesterDay(companyCode);
        return monthValue;
    }


}
